﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Ession
{
    /// <summary>
    /// AutoPost修改版,以后应多使用此类
    /// </summary>
    public class HttpRequestSender
    {
        public HttpRequestSender(int maxLimit=50)
        {
            CookieContainer = new CookieContainer();
            ServicePointManager.MaxServicePoints = maxLimit;
            ServicePointManager.DefaultConnectionLimit = maxLimit;
        }

         
        public int PageSize { get; set; }


        public string URL = "";
        public CookieContainer CookieContainer { get; set; }
        /// <summary>
        /// 以Get方式访问网址
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public string GetHtml(string URL, Encoding en = null, int timeOut = 12000, IWebProxy proxy = null)
        {
            try
            {
                HttpWebRequest request;
                HttpWebResponse response;
                request = HttpWebRequest.Create(URL) as HttpWebRequest;
                request.Method = "GET";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)";
                request.Timeout = timeOut;
                request.CookieContainer = CookieContainer;
                if (proxy != null)
                {
                    request.Proxy = proxy;
                    request.UseDefaultCredentials = true;
                }
                response = request.GetResponse() as HttpWebResponse;
                //     response.Headers.AllKeys.Contains("")
                if (en == null)
                {
                    return new StreamReader(response.GetResponseStream()).ReadToEnd();
                }

                return new StreamReader(response.GetResponseStream(), en).ReadToEnd();


            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        public string GetHtml(string URL, string userAgent = null, Encoding en = null, int timeOut = 12000, IWebProxy proxy = null)
        {
            try
            {
                HttpWebRequest request;
                HttpWebResponse response;
                request = HttpWebRequest.Create(URL) as HttpWebRequest;
                request.Method = "GET";
                if (userAgent != null)
                {
                    request.UserAgent = userAgent;
                }
                else
                {
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)";
                }
                request.Timeout = timeOut;
                request.CookieContainer = CookieContainer;
                if (proxy != null)
                {
                    request.Proxy = proxy;
                    request.UseDefaultCredentials = true;
                }
                response = request.GetResponse() as HttpWebResponse;
                //     response.Headers.AllKeys.Contains("")
                if (en == null)
                {
                    return new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                else
                {
                    return new StreamReader(response.GetResponseStream(), en).ReadToEnd();
                }

            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public string PostData(string URL, string postData, Encoding en, int timeOut = 10000, IWebProxy proxy = null)
        {
            try
            {
                HttpWebRequest request;
                HttpWebResponse response;
                request = HttpWebRequest.Create(URL) as HttpWebRequest;
                request.Method = "POST";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)";
                request.ContentType = "application/x-www-form-urlencoded";
                if (proxy != null)
                {
                    request.Proxy = proxy;
                    request.UseDefaultCredentials = true;

                }
                request.Timeout = timeOut;
                request.CookieContainer = CookieContainer;
                byte[] postBytes = en.GetBytes(postData);
                request.ContentLength = postBytes.Length;
                Stream newStream = request.GetRequestStream();
                newStream.Write(postBytes, 0, postBytes.Length);
                newStream.Close();

                response = (HttpWebResponse)request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, en);
                return reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                return "";
            }

        }
        /// <summary>
        /// 只获取响应头部信息,满足则返回true
        /// </summary>

        public bool PostData(string URL, string postData, Encoding en, string headerKey, Func<string, bool> cc, int timeOut = 10000, IWebProxy proxy = null)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            request = HttpWebRequest.Create(URL) as HttpWebRequest;

            request.Method = "POST";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)";
            request.ContentType = "application/x-www-form-urlencoded";
            if (proxy != null)
            {
                request.Proxy = proxy;
                request.UseDefaultCredentials = true;

            }
            request.Timeout = timeOut;
            request.CookieContainer = CookieContainer;// new CookieContainer();
            byte[] postBytes = en.GetBytes(postData);
            request.ContentLength = postBytes.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(postBytes, 0, postBytes.Length);
            newStream.Close();
          
            //post 结束
            //返回HTML
            response = (HttpWebResponse)request.GetResponse();

           
            if (cc(response.GetResponseHeader(headerKey)))
            {
                response.Close();
                return true;
            }
            response.Close();
          
            return false;

        }

    }
}
